package com.ldc.framework.activity

import android.content.Context
import android.content.Intent
import android.os.Message
import android.view.View
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.ldc.commonlibs.listener.OnRowListener
import com.ldc.commonlibs.uts.ApiUtils
import com.ldc.commonlibs.uts.MessageUts
import com.ldc.commonlibs.uts.ToastUtils
import com.ldc.commonlibs.uts.ViewModelUtils
import com.ldc.commonres.common.VBMActivity
import com.ldc.framework.adapter.CoinAdapter
import com.ldc.framework.databinding.FrameworkActivityCoInBinding
import com.ldc.framework.viewmodel.FrameworkViewModelBase
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener

class CoInActivity : VBMActivity<FrameworkActivityCoInBinding, FrameworkViewModelBase>() {
    private var page: Int = 1
    private var coinAdapter: CoinAdapter? = null

    companion object {
        private const val ARG_TITLE = "title"
        private const val REFRESH_CODE = 0x1231
        fun start(context: Context, title: String) {
            context.startActivity(Intent(context, CoInActivity::class.java).apply {
                putExtra(ARG_TITLE, title)
            })
        }

        fun start(context: Fragment, title: String) {
            start(context.requireContext(), title)
        }
    }

    override fun processMessage(msg: Message): Boolean {
        when (msg.what) {
            REFRESH_CODE -> {
                getCoinList()
            }
        }
        return super.processMessage(msg)
    }


    override fun main() {
        initAdapter()
        viewBind?.topBar?.let {
            it.flBack.setOnClickListener { finish() }
            it.tvName.text = intent?.run {
                getStringExtra(ARG_TITLE)
            }
        }
        viewBind?.contentView?.refreshView?.setOnRefreshLoadMoreListener(object :
            OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page = 1
                MessageUts.sendMessage(getHandler(), REFRESH_CODE)
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                page += 1
                MessageUts.sendMessage(getHandler(), REFRESH_CODE)
            }
        })
        viewBind?.statusView?.let {
            it.showLoadView()
            it.addRetryClickListener(object : OnRowListener<Int> {
                override fun onRow(v: View, data: Int) {
                    it.showLoadView("正在重试，请稍等")
                    page = 1
                    MessageUts.sendMessage(getHandler(), REFRESH_CODE)
                }
            })
        }
        MessageUts.sendMessage(getHandler(), REFRESH_CODE)
    }

    override fun createViewBinding(): FrameworkActivityCoInBinding =
        FrameworkActivityCoInBinding.inflate(layoutInflater)

    override fun createViewModel(): FrameworkViewModelBase =
        ViewModelUtils.create(this, FrameworkViewModelBase::class.java)


    private fun getCoinList() {
        viewModel?.coinList(page)?.observe(this) {
            viewBind?.statusView?.showContentView()
            viewBind?.contentView?.refreshView?.let { rf ->
                if (rf.state.isOpening) {
                    rf.finishRefresh()
                    rf.finishLoadMore()
                }
            }
            if (ApiUtils.isSuccess(it)) {
                if (page == 1) {
                    coinAdapter?.newData(it.data?.datas)
                } else {
                    coinAdapter?.addNewData(it.data?.datas)
                }
            } else {
                viewBind?.statusView?.showErrorView(it.errorMsg)
            }
            if (coinAdapter?.isEmptyData()!!) {
                viewBind?.statusView?.showEmptyView()
            }
        }
    }


    private fun initAdapter() {
        coinAdapter = CoinAdapter()
        viewBind?.contentView?.let { cv ->
            cv.rv.recyclerview.also { rv ->
                rv.adapter = coinAdapter
                rv.layoutManager =
                    LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false)
                rv.setHasFixedSize(true)

            }

        }
    }

}